﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using pub.Functions;
using pub.Interop;

namespace scheduler
{
    internal static class Program
    {
        /// <summary>
        ///   The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            if (Test())
            {
                return;
            }
            
            //
            bool mutexCreated;
            var mutexName = Consts.InstanceMutexName;
            using (new Mutex(true, mutexName, out mutexCreated))
            {
                if (!mutexCreated)
                {
                    NotifyPrevInstance();
                }
                else
                {
                    CreateInstance();
                }
            }
        }

        private static void NotifyPrevInstance()
        {
            var hwnd = IntPtr.Zero;
            for (var n = 0; n < 10 && hwnd == IntPtr.Zero; n++)
            {
                hwnd = Win32.FindWindow(null, Consts.MessageSinkWindowTitle);
                Thread.Sleep(200);
            }
            if (hwnd == IntPtr.Zero)
            {
                MessageBox.Show("something wrong.\ntodo_scheduler_message_sink not found.. ");
                return;
            }
            Win32.SendMessage(hwnd, Consts.WM_APP_ACTIVATE, 0, 0);

            var args = Environment.GetCommandLineArgs();
            for(var n = 1; n < args.Length; n++)
            {
                if (args[n] == "/add")
                {
                    var fileName = args[n + 1];
                    NotifyAdd(hwnd, fileName);
                }
            }
        }

        private static void NotifyAdd(IntPtr hwnd, string fileName)
        {
            var cds = new Win32.COPYDATASTRUCT {
                dwData = (IntPtr) Consts.COPY_DATA_TYPE_ADD,
                cbData = fileName.GetByteCount() + 1,
                lpData = fileName
            };
            Win32.SendMessage(hwnd, Win32.WM_COPYDATA, 0, ref cds);
        }

        private static void CreateInstance()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            App.Initialize();
            if (test())
            {
                return;
            }

            App.MainForm = new MainForm();
            Application.Run();
        }

        private static bool test()
        {
            return false;

            var todoListFile = @"D:\prj\spike\todo\todo\test\test.todolist";
            var reminder = new RemindChecker(todoListFile);

            var checkTime = DateTime.Now;
            checkTime = new DateTime(2012, 12, 20, 10, 1, 0);

            var lastCheckTime = checkTime.AddMinutes(-1);
            lastCheckTime = checkTime.AddDays(-10);

            reminder.Check(lastCheckTime, checkTime);

            return true;
        }

        private static bool Test()
        {
            bool noTest = false;
            noTest = true;
            if (noTest)
            {
                return false;
            }
            
            Console.WriteLine("?");

            return true;
        }
    }
}